// packageA/pages/performs/performOrder/performOrder.js
const { post } = require("../../../../public.js");
import http from '../../../../utils/http.js';
var common = require("../../../public.js");
Page({

    /**
     * 页面的初始数据
     */
    data: {
        selectedBuyerList: [], // 选中的购票人
        value: '',
        totalPrice:0.00,
        showBuyerPanel: false,
        showCouponsPanel: false,
        isLoadingBusy: false,
        ticketName:'',
        storeName:'',
        ticketPrice:"",
        ticketNum:"",
        date_box:"",
        holdingAddress:"",
        ticketTimeId:"",
        ticketPriceId:"",
        performTimeDisplay:"",
        performTime:"",
        totalPrice:"",
        totalMoney:""
    },
    onNameChange(e){
        this.setData({
            name: e.detail
          })
    },
      // 手机号
      onPhoneChange(e) {
        this.setData({
          phone: e.detail
        })
      },
    onSubmit(){
        const that = this
        if (that.data.selectedBuyerList.length != that.data.ticketNum) {
          wx.showToast({
            title: '请添加' + that.data.ticketNum + '位购票人信息',
            icon: 'none'
          })
          return;
        }
        if (!that.data.name) {
            wx.showToast({
              title: '请添加购票人姓名',
              icon: 'none'
            })
            return;
          }
          if (!that.data.phone) {
            wx.showToast({
              title: '请添加购票人手机号',
              icon: 'none'
            })
            return;
          }
          let buyerList = []
          that.data.selectedBuyerList.map(index => {
            let buyerItem = {
              name: that.data.buyerList[index].name,
              phone: that.data.buyerList[index].phone,
              certificateNo: that.data.buyerList[index].certificateNo,
            }
            buyerList.push(buyerItem)
          })
          let tempJson = {
            storeId: that.data.storeId,
            admissionType:'1' , // 1电子票
            couponNo: '', //省惠民券编码
            cityCouponNo: that.data.currentCoupon ? that.data.currentCoupon.item.couponNo : '',
            discountMoney:0 ,
            payment: Number(that.data.totalPrice),
            status:'1',
            userId: that.data.userInfo.id,
            userTicketCollectionName:that.data.name,
            userTicketCollectionPhone:that.data.phone,
            ticketId:that.data.ticketId,
            ticketName: that.data.ticketName,
            ticketPrice: Number(that.data.ticketPrice),
            ticketNum: that.data.ticketNum,
            token: that.data.token,
            ticketTimeId:that.data.ticketTimeId,  //时间id
            ticketPriceId:that.data.ticketPriceId,
            performTime:that.data.performTime,
            benefitFlag:false,
            buyerList: buyerList,
        
          }
         
          // 第三种
          common.postJson("/storePerformOrder/buyTicket", tempJson, function (res) {
            if (res.status == 200) {
              if (that.data.totalPrice == 0) {
                wx.navigateTo({
                  url: '/pages/ScenicArea/ScenicAreaCommitPage/paySuccess/index?payment=' + that.data.totalPrice +
                  '&cardId=' + 'yanchu'
                })
              } else {
                that.wxpay(res.data)
              }
            } else {
              common.showRequestErrMsg(res)
            }
          })

    },
    wxpay(orderId) { // 支付
      let that = this
      let tempJson = {
        orderId,
        userId: that.data.userInfo.id,
        token: that.data.token,
        openid: that.data.openid
      }
      common.wxPayRequest("/wxxcxPayForPerformTicket", tempJson, function (res) {
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          'success': function (res) {
            common.showToast('支付成功', 'success')
            wx.navigateTo({
              url: '/pages/ScenicArea/ScenicAreaCommitPage/paySuccess/index?payment=' + that.data.totalPrice + '&orderId=' + orderId 
              + '&cardId=' + 'yanchu',
            })
          },
          'fail': function (res) {
            if (res.errMsg === 'requestPayment:fail cancel') {
              wx.redirectTo({
                url: '/packageA/pages/performs/performOrderList/performOrderList?status=1',
              })
            } else {
              common.showToast(res.errMsg)
            }
          }
        })
      })
  
    },
    onChange(event) {
        // event.detail 为当前输入的值
        console.log(event.detail);
      },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        console.log(options)
        common.getLocalUserInfo(function (success, data) {
          if (success) {
            that.setData({
              userInfo: data.user,
              token: data.token,
              openid: data.openid,
              ticketId: options.storePerformId, //门票
              storeId: options.storeId,
              ticketName: options.ticketName,
              storeName: options.storeName,
              ticketPrice: options.ticketPrice,
              ticketNum: options.ticketNum,
              date_box: options.date_box,
              holdingAddress: options.holdingAddress ?options.holdingAddress :'',
              ticketTimeId: options.ticketTimeId,
              ticketPriceId:options.ticketPriceId,
              performTimeDisplay:options.performTimeDisplay,
              performTime:options.performTime,
              totalPrice: options.ticketPrice  * options.ticketNum,
              totalMoney: options.totalMoney
            }, _ => {
              that.getBuyerList()
            })
          } else{
             common.showLoginPage()
          }
          })
     
    },
      getBuyerList() { // 获取购票人列表
          let that = this
          let tempJson = {
            userId: that.data.userInfo.id,
            token: that.data.token
          }
          common.post("/scenicTicket/ticketBuyer/getScenicTicketBuyerList", tempJson, function (res) {
            if (res.status === 200) {
              that.setData({
                buyerList: res.data
              })
            }
          })
        },
      addBuyer() {
        this.setData({
          showBuyerPanel: true
        })
      },
      closeBuyerPanel() {
        this.setData({
          showBuyerPanel: false
        })
      },
      selectBuyer(e) {
        console.log(e)
        let {
          index,
          item
        } = e.currentTarget.dataset
    
        let arrIndex = this.data.selectedBuyerList.indexOf(index)
        if (arrIndex != -1) { // 有
    
          let arr = this.data.selectedBuyerList
          arr.splice(arrIndex, 1)
          console.log(arr)
          this.setData({
            selectedBuyerList: arr
          })
    
        } else { // 没有
          if (this.data.selectedBuyerList.length < this.data.ticketNum) {
            this.setData({
              selectedBuyerList: [...this.data.selectedBuyerList, index]
            })
          }
        }
      },
      editBuyer(e) {
        let {
          item
        } = e.currentTarget.dataset
        console.log(item)
        wx.navigateTo({
          url: '/pages/ScenicArea/ScenicAreaAddBuyer/index?buyerInfo=' + JSON.stringify(item),
        })
      },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})